import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  // 用户路由
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/user/Home.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/user/center',
    name: 'UserCenter',
    component: () => import('../views/user/UserCenter.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/user/order/create',
    name: 'OrderCreate',
    component: () => import('../views/user/OrderCreate.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/user/order/list',
    name: 'OrderList',
    component: () => import('../views/user/OrderList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/user/order/detail',
    name: 'OrderDetail',
    component: () => import('../views/user/OrderDetail.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/user/promotions',
    name: 'Promotions',
    component: () => import('../views/user/Promotions.vue'),
    meta: { requiresAuth: true }
  },
  // 管理员路由
  {
    path: '/admin/home',
    name: 'AdminHome',
    component: () => import('../views/admin/Home.vue'),
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/admin/users',
    name: 'UserManagement',
    component: () => import('../views/admin/UserManagement.vue'),
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/admin/orders',
    name: 'OrderManagement',
    component: () => import('../views/admin/OrderManagement.vue'),
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/admin/announcements',
    name: 'AnnouncementManagement',
    component: () => import('../views/admin/AnnouncementManagement.vue'),
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/admin/news',
    name: 'NewsManagement',
    component: () => import('../views/admin/NewsManagement.vue'),
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/admin/services',
    name: 'ServiceManagement',
    component: () => import('../views/admin/ServiceManagement.vue'),
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/admin/clothes',
    name: 'ClothesManagement',
    component: () => import('../views/admin/ClothesManagement.vue'),
    meta: { requiresAuth: true, requiresAdmin: true }
  }
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const user = JSON.parse(localStorage.getItem('user') || '{}')
  
  if (to.meta.requiresAuth && !user.id) {
    next('/login')
  } else if (to.meta.requiresAdmin && !user.isAdmin) {
    next('/home')
  } else {
    next()
  }
})

export default router 